// const request = require("request")
const app = getApp()
// pages/sort/sort.js
const {
  getCartList,
  getProductList,
  getSearchGoodsList, //搜索
  getAddShopping,//加入购物车
  getGoodsDetail, //商品详情

} = require('../../http/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    sortList: [],
    // 商品列表
    productList: [],
    // 
    productListId: 322089,
    list: [],
    infor: '',
    // 加入购物车的参数
    obj: {},
    id: '',
    show:false,
    properties:[],
    lists:{},
    choseId: [],
    sku:[]
  },
  // 加入购物车
  async addShopping(e) {
    // console.log(e);
    this.setData({
      id: e.currentTarget.dataset.id
    })
    this.data.obj = {
      goodsId: this.data.id,
      number: 1,
      token:wx.getStorageSync('token')
    }
    this.setData({
      obj: this.data.obj
    })
    // console.log(this.data.obj)
    const shopping = await getAddShopping(this.data.obj)
    console.log(shopping);
    // console.log(this.data.shopping);
    if(shopping.code===0){
      wx.showToast({
        title: '成功加入购物车',
      })
    }
    if(shopping.code===30002){
      this.setData({
        show:true
      })
      wx.hideTabBar({
        animation: false,
      })

      const aaa = await getGoodsDetail(e.currentTarget.dataset.id)
      aaa.data.properties.forEach(item => {
        item.childsCurGoods.forEach(val => {
          val.flag = false
        })
      })
    this.setData({
      lists:aaa.data.basicInfo,
      properties:aaa.data.properties
    })

    // console.log(this.data.properties)

    // 


    }

  },
  // 选中和未选中
  choseItem(e) {
    const { index, i } = e.currentTarget.dataset
    this.data.properties[index].childsCurGoods.forEach(item => {
      item.flag = false
    })
    this.data.properties[index].childsCurGoods[i].flag = !this.data.properties[index].childsCurGoods[i].flag
    this.setData({
      properties: this.data.properties,
    })
  },
  // 添加购物车，拿sku的值
  async addCart() {
    const arr = this.data.properties.map(item => {
      return item.childsCurGoods.filter(item => item.flag === true)
    })
    const add = this.data.properties.map(item=>{
      return item.id
    })
    // console.log(add);

    const aa = arr.flat(Infinity).map(item=>{
      return item.id
    })
    // console.log(arr.length);
    // console.log(this.data.properties.length);
    if(arr.length != this.data.properties.length){
      wx.showLoading({
        title: '层级不够',
      })
      return
    }
    // console.log(add);
    // console.log(aa);
    let obj =[]
    let size = add.length
    for(let i = 0 ;i < size ; i ++){
      let a = {}
      a.optionId = add[i]
      a.optionValueId=aa[i]
      obj.push(a)
    }
    // console.log(obj);
    // console.log(obj);
    const sku = JSON.stringify(obj)
    // console.log(sku);
    // wx.setStorageSync('key', JSON.stringify)
    const abc = await getAddShopping({goodsId:this.data.id,number:1,sku,token:wx.getStorageSync('token')})

    wx.showToast({
      title: '成功加入购物车',
    })
    // console.log(abc);
    // console.log(obj);
    this.setData({
      show:false,

    })
    wx.showTabBar({
      animation: false,
    })
  },
  goDetail(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },
  async cutList(e) {
    const res6 = await getProductList()
    this.setData({
      productListId: e.currentTarget.dataset.item.id
    })
    this.data.list = this.data.productList.filter(item => item.categoryId === this.data.productListId)
    this.setData({
      list: this.data.list
    })

  },
  async search(e) {

    this.setData({
      infor: e.detail
    })

    wx.navigateTo({
      url: '/pages/searchGoodsList/searchGoodsList?orderBy=' + e.detail,
    })
    this.setData({
      infor: ''
    })


  },
  // // 显示弹框
  // async showpopup(e){
  //   this.setData({
  //     show:true
  //   })
  //   wx.hideTabBar({
  //     animation: false,
  //   })
  //   // console.log(e.currentTarget.dataset.item);
  //   this.setData({
  //     skuList:e.currentTarget.dataset.item
  //   })
  //   const aaa = await getGoodsDetail(e.currentTarget.dataset.id)
  //   console.log(aaa);
  // },
  // 
  onClose() {
    this.setData({
      show:false
    })
    wx.showTabBar({
      animation: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    // console.log(app.globalData.index);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    const res = await getCartList()
    // console.log(res);
    this.setData({
      sortList: res.data
    })
    // this.List(this.data.sortList[0].id)
    // console.log(this.data.sortList);

    // 商品列表
    const res6 = await getProductList()
    // console.log(res6);
    this.setData({
      productList: res6.data.result
    })
    // console.log(this.data.productList);

    // cosnt index = app.globalData.index ? app.globalData.index : 0;
    // console.log(app);
    this.setData({
      activeKey: app.globalData.index
    })
    this.data.list = this.data.productList.filter(item => item.categoryId === this.data.sortList[this.data.activeKey].id)
    // console.log(this.data.sortList[this.data.activeKey].id);
    this.setData({
      list: this.data.list
    })
    // console.log(this.data.list);





    this.data.list = this.data.productList.filter(item => item.categoryId === this.data.sortList[0].id)
    this.setData({
      list: this.data.list
    })
  },
  // async List(id) {
  //   console.log(id);
  //   const res2 = await getProductList('/shop/goods/list/v2',{categoryId:id},'POST')
  //   // console.log(res2);
  // },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})